package com.study.quiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.quiz.dto.NotifictionDTO;
import com.study.quiz.dto.PageDTO;
import com.study.quiz.dto.QuestionDTO;
import com.study.quiz.enums.NotifictionEnum;
import com.study.quiz.enums.NotifictionStatusEnum;
import com.study.quiz.mapper.NotificationMapper;
import com.study.quiz.pojo.Notification;
import com.study.quiz.pojo.Question;
import com.study.quiz.pojo.User;
import com.study.quiz.service.NotifictionService;
import com.study.quiz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author CJunGod
 * @create 2020-03-13 19:48
 **/
@Slf4j
@Service
@Transactional
public class NotifictionServiceImpl implements NotifictionService {

    @Resource
    private UserService userService;

    @Resource
    private NotificationMapper notificationMapper;

    @Override
    public Integer insert(Notification notification) {

        return notificationMapper.insert(notification);
    }

    @Override
    public PageDTO findByUserId(Integer userId, Integer page, Integer size) {
        Integer count = notificationMapper.selectCount(new QueryWrapper<Notification>().eq("receiver", userId));
        Integer totalPage;
        PageDTO<NotifictionDTO> pageDTO = new PageDTO<>();
        if (count % size == 0) {
            totalPage = count / size;
        } else {
            totalPage = count / size + 1;
        }
        pageDTO.setTotalPage(totalPage);

        if (page < 1) {
            page = 1;
        }
        if (page > totalPage && totalPage != 0) {
            page = totalPage;
        } else if (totalPage == 0) {
            page = 1;
        }

        PageHelper.startPage(page, size);
        PageInfo<Notification> pageInfo = new PageInfo<>(notificationMapper.selectList(new QueryWrapper<Notification>()
                .eq("receiver", userId)
                .orderByDesc("gmt_create")));
        List<NotifictionDTO> dtoList = new ArrayList<>();
        pageInfo.getList().forEach(q -> {
            User user = userService.findByUserId(q.getReceiver().intValue());
            NotifictionDTO dto = new NotifictionDTO();
            dto.setNotifier(user.getId().longValue());
            dto.setTypeName(NotifictionEnum.nameOfType(q.getType()));
            dto.setNotifierName(user.getName());
            dto.setGmtCreate(q.getGmtCreate());
            dto.setStatus(q.getStatus());
            dto.setType(q.getType());
            dto.setId(q.getId());
            dto.setOuterTitle(q.getOuterTitle());
            dtoList.add(dto);
        });

        pageDTO.setDtoList(dtoList);

        List<Integer> pageList = new ArrayList<>();
        pageList.add(page);

        for (int i = 1; i <= 3; i++) {
            if (page - i > 0) {
                pageList.add(0, page - i);
            }

            if (page + i <= pageInfo.getPages()) {
                pageList.add(page + i);
            }
        }

        pageDTO.setPages(pageList);
        pageDTO.setPage(page);

        if (pageInfo.getPageNum() == 1) {
            pageDTO.setShowPrevious(false);
        } else {
            pageDTO.setShowPrevious(true);
        }

        if (pageInfo.getPages() == page || totalPage == 0) {
            pageDTO.setShowNet(false);
        } else {
            pageDTO.setShowNet(true);
        }

        if (pageDTO.getPages().contains(1)) {
            pageDTO.setShowFirstPage(false);
        } else {
            pageDTO.setShowFirstPage(true);
        }

        if (pageDTO.getPages().contains(pageInfo.getPages()) || totalPage == 0) {
            pageDTO.setShowEndPage(false);
        } else {
            pageDTO.setShowEndPage(true);
        }

        return pageDTO;

    }

    @Override
    public Long unreadCount(Integer userId) {

        return notificationMapper.selectCount(new QueryWrapper<Notification>()
                .eq("receiver", userId)
                .eq("status",NotifictionStatusEnum.UNREAD.getStatus())).longValue();
    }

    @Override
    public NotifictionDTO read(Long notificationId, User user) {
        Notification notification = notificationMapper.selectById(notificationId);
        if(!Objects.equals(notification.getReceiver(),user.getId().longValue())){
                return new NotifictionDTO();
        }

        notification.setStatus(NotifictionStatusEnum.READ.getStatus());
        notificationMapper.updateById(notification);

        NotifictionDTO notifictionDTO = new NotifictionDTO();
        BeanUtils.copyProperties(notification, notifictionDTO);
        notifictionDTO.setTypeName(NotifictionEnum.nameOfType(notification.getType()));

        return notifictionDTO;
    }
}
